package com.nd.android.u.ui.activity.message_chat;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nd.android.u.controller.innerInterface.IBottomFunction;
import com.nd.android.u.ui.R;

/**
 * 聊天应用消息适配器（聊天界面底部点击更多按钮出现的）
 * <br>Created 2014-6-9 下午1:53:06
 * @version  
 * @author   cb		
 * @see 	 
 */
public class ChatAppAdapter extends BaseAdapter{
	
	private Context mContext;
	private List<IBottomFunction> mAppList = null;
	private ChangeTypefaceCallBack mCallBack;
	
	/**
	 * 设置改变字体回调
	 * <br>Created 2014-9-17 下午11:10:45
	 * @param callBack 改变字体回调
	 * @author  cb
	*/
	public void setChangeTypefaceCallBack(ChangeTypefaceCallBack callBack){
		mCallBack=callBack;
	}
	/**
	 * 获取应用列表
	 * <br>Created 2014-6-9 下午1:53:40
	 * @return List<IListGridItem>
	 * @author       cb
	*/
	public List<IBottomFunction> getAppList() {
		return mAppList;
	}

	/**
	 * 设置应用列表
	 * <br>Created 2014-6-9 下午1:53:50
	 * @param appList 应用列表
	 * @author       cb
	*/
	public void setIListGridItemList(List<IBottomFunction> appList) {
		this.mAppList = appList;
	}

	
	/**
	 * Creates a new instance of ChatAppAdapter.
	 * <br>Created 2014-6-9 下午1:54:05
	 * @param context 上下文
	 */
	public ChatAppAdapter(Context context){
		this.mContext = context;
		mAppList = new ArrayList<IBottomFunction>();
	}

	@Override
	public int getCount() {
		if(mAppList==null) {
			return 0;
		}
		return mAppList.size();
	}

	@Override
	public Object getItem(int position) {
		if(mAppList==null) {
			return null;
		}
		return mAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	/**
	 * holder
	 * <br>Created 2014-6-9 下午1:54:36
	 * @version  ChatAppAdapter
	 * @author   cb		
	 * @see 	 
	 */
	class Holder{
		ImageView image;
		TextView appname;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Holder holder;
		if(convertView==null){
			holder = new Holder();
			LayoutInflater inflate = LayoutInflater.from(mContext);
			convertView = inflate.inflate(R.layout.chat_app_item, null);
			holder.image = (ImageView) convertView.findViewById(R.id.chat_app_img);
			holder.appname = (TextView) convertView.findViewById(R.id.chat_app_name);
			if (mCallBack!=null){
				mCallBack.changeType(holder.appname);
			}
			convertView.setTag(holder);
		}
		holder = (Holder) convertView.getTag();
		IBottomFunction app = mAppList.get(position);
		if(app.getAppResId()!=0){
			holder.image.setImageResource(app.getAppResId());
		}
		holder.appname.setText(app.getLabel());
		return convertView;
	}
}